package com.hs.sfzy.service.impl;

import com.hs.sfzy.entity.CourseCollect;
import com.hs.sfzy.entity.User;
import com.hs.sfzy.mapper.CourseCollectMapper;
import com.hs.sfzy.mapper.UserMapper;
import com.hs.sfzy.service.CourseCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseCollectServiceImpl implements CourseCollectService {
    @Autowired
    private CourseCollectMapper courseCollectMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(int userId, int courseId) {
        CourseCollect courseCollect = new CourseCollect();
        courseCollect.setUserId(userId);
        courseCollect.setCourseId(courseId);
        courseCollect.setIsok(0); // 默认未支付
        // 确保Mapper的insert方法参数为CourseCollect类型
        return courseCollectMapper.insert(courseCollect);
    }

    @Override
    public int exists(int userId, int courseId) {
        return courseCollectMapper.exists(userId, courseId);
    }

    @Override
    public int updatePayStatus(int userId, int courseId, int isok) {
        // 校验状态值合法性
        if (isok != 0 && isok != 1) {
            throw new IllegalArgumentException("isok必须为0或1");
        }
        return courseCollectMapper.updatePayStatus(userId, courseId, isok);
    }

    @Override
    public List<CourseCollect> selectWithRelations(int userId, int payStatus) {
        // 校验状态值合法性
        if (payStatus != 0 && payStatus != 1) {
            throw new IllegalArgumentException("payStatus必须为0或1");
        }
        return courseCollectMapper.selectWithRelations(userId, payStatus);
    }

    @Override
    public int delete(Integer userId, Integer courseId) {
        return courseCollectMapper.delete(userId, courseId);
    }

    @Override
    public List<CourseCollect> getCourseCollectByUserId(Integer userId) {
        // 校验用户ID非空
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        return courseCollectMapper.getCourseCollectByUserId(userId);
    }

    @Override
    public Integer addCourseCollect(CourseCollect courseCollect) {
        // 校验参数非空
        if (courseCollect == null || courseCollect.getUserId() == null || courseCollect.getCourseId() == null) {
            throw new IllegalArgumentException("用户ID和课程ID不能为空");
        }

        User user = userMapper.findUserById(courseCollect.getUserId());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 检查是否已收藏
        int existCount = courseCollectMapper.exists(
                courseCollect.getUserId(),
                courseCollect.getCourseId()
        );
        if (existCount > 0) {
            throw new RuntimeException("该课程已收藏");
        }

        // 修正状态设置逻辑：如果未设置则默认为0
        if (courseCollect.getIsok() < 0) {
            courseCollect.setIsok(0);
        }

        int insertCount = courseCollectMapper.addCourseCollect(courseCollect);
        if (insertCount > 0) {
            // 增加积分（避免负数）
            user.setPoints(Math.max(0, user.getPoints() + 10));
            userMapper.updateUser(user);
        }

        return insertCount;
    }

    @Override
    public Integer addCourseCollectByUidAndCid(Integer userId, Integer courseId) {
        // 校验参数非空
        if (userId == null || courseId == null) {
            throw new IllegalArgumentException("用户ID和课程ID不能为空");
        }

        int existCount = courseCollectMapper.exists(userId, courseId);
        if (existCount > 0) {
            throw new RuntimeException("该课程已在列表中");
        }

        int insertCount = courseCollectMapper.addCourseCollectByUidAndCid(userId, courseId);
        if (insertCount > 0) {
            User user = userMapper.findUserById(userId);
            if (user != null) {
                // 增加积分（避免负数）
                user.setPoints(Math.max(0, user.getPoints() + 10));
                userMapper.updateUser(user);
            }
        }

        return insertCount;
    }

    @Override
    public CourseCollect checkIsCollected(Integer userId, Integer courseId) {
        return courseCollectMapper.checkIsCollected(userId, courseId);
    }
}
    